	<?php
	function advancedRmdir($path) {
    $origipath = $path;
    $handler = opendir($path);
    while (true) {
        $item = readdir($handler);
        if ($item == "." or $item == "..") {
            continue;
        } elseif (gettype($item) == "boolean") {
            closedir($handler);
            if (!@rmdir($path)) {
                return false;
            }
            if ($path == $origipath) {
                break;
            }
            $path = substr($path, 0, strrpos($path, "/"));
            $handler = opendir($path);
        } elseif (is_dir($path."/".$item)) {
            closedir($handler);
            $path = $path."/".$item;
            $handler = opendir($path);
        } else {
            unlink($path."/".$item);
        }
    }
    return true;
	}

	print '<script type="text/javascript">
			<!--
			function confirmation_s() {
				var answer = confirm("Are you sure you want to delete this style?")
				if (answer) {
				window.location = "?page='.$_GET['page'].'&style='.$this->options['style'].'&do=delete";
				} else {
				return false;
				}
			}
	//-->
	</script>';

	if ($_POST['ss_action'] == 'save' && !empty($_GET['style']) && !empty($_GET['css']) && $_GET['do'] == 'edit' && file_exists(TEMPLATEPATH.'/styles/'. $_GET['style'] .'/'. $_GET['css'] .'.css') && is_writable(TEMPLATEPATH.'/styles/'. $_GET['style'] .'/'. $_GET['css'] .'.css')) {
			if ($file = fopen(TEMPLATEPATH.'/styles/'.$_GET['style'].'/'.$_GET['css'].'.css', 'w')) {
				$updated_line = stripslashes($_POST["css_c"]);
				fwrite($file, "$updated_line");
				$msg = '<p style="background:green;display:inline;color:#fff;font-weight:bold;padding: 1px;">Your file has been edited successfully!</p>';
			} else {
				$msg = '<p style="background:red;display:inline;color:#fff;font-weight:bold;padding: 1px;">Your file has not been edited!</p>';
			}
			fclose($file);
	}
	
	if ($_POST['ss_action'] != 'save' && !empty($_GET['style']) && $_GET['do'] == 'delete') {
		$dirPath = TEMPLATEPATH. '/styles/';
		if ($handle = opendir($dirPath)) {
			while (false !== ($file = readdir($handle))) {
				if ($file != "." && $file != "..") {
					if (is_dir("$dirPath/$file")) {
						$getstyles[] = $file;
					}
				}
			}
		closedir($handle);
		}
		if (!is_dir($dirPath.$this->options['style'])) {
		$msg = '<p style="background:red;display:inline;color:#fff;font-weight:bold;padding: 1px;">This style does not exist or it might have been deleted already!</p>';
		} elseif ($_GET['style'] == $this->default_settings['style']) {
		$msg = '<p style="background:red;display:inline;color:#fff;font-weight:bold;padding: 1px;">You cannot delete the default theme style!</p>';
		} else {
		$msg = '<p style="background:green;display:inline;color:#fff;font-weight:bold;padding: 1px;">Style deleted successfully. Please save changes now!</p>';
		advancedRmdir($dirPath.$this->options['style'].'/');
		}
	}
	
	if ($_POST['ss_action'] != 'save' && !empty($_GET['style']) && $_GET['do'] == 'edit' && !is_dir(TEMPLATEPATH.'/styles/'.$_GET['style'])) {
	$msg = '<p style="background:red;display:inline;color:#fff;font-weight:bold;padding: 1px;">You are trying to edit a deleted / non-exist style. Please save changes first and select an existing style!</p>';
	}
	?>
	
	<div id="9" class="cptab">
	<h2>Style Manager</h2>
	<p>Each style contains CSS and images and has a unique folder name inside <b>styles</b> directory. The default style comes with the theme already, but you can create as many styles as you wish.</p>
	
	<fieldset><legend>Default Style</legend>
	<label for="cp_style">Choose from available styles</label>
	<select name="cp_style" class="widefat big" onchange="this.form.submit();">
	<?php
	$dirPath = TEMPLATEPATH. '/styles/';
	if ($handle = opendir($dirPath)) {
	while (false !== ($file = readdir($handle))) {
	if ($file != "." && $file != "..") {
		if (is_dir("$dirPath/$file")) {
		?>
		<option value="<?php echo $file; ?>"<?php selected($file, $this->options["style"]); ?>><?php echo ucfirst($file); ?></option>
		<?php
		}
	}
	}
	closedir($handle);
	}
	?>
	</select><div class="icons-c"><a title="Under development"><img src="<?php bloginfo('template_directory'); ?>/cp/img/icon_new_ds.png" alt="Duplicate style" /></a><a href="?page=<?php echo $_GET['page']; ?>&amp;style=<?php echo $this->options['style']; ?>&amp;do=edit" title="Edit style"><img src="<?php bloginfo('template_directory'); ?>/cp/img/icon_edit.gif" alt="Edit style" /></a><a href="#" title="Delete style" onclick="javascript:confirmation_s();"><img src="<?php bloginfo('template_directory'); ?>/cp/img/icon_delete.gif" alt="Delete style" /></a></div>
	<div class="clear"></div>
	</fieldset>
	
	<?php if (!empty($_GET['style']) && $_GET['do'] == 'edit' && is_dir(TEMPLATEPATH.'/styles/'.$_GET['style'])) { ?>
	<fieldset><legend><?php echo ucfirst($this->options["style"]); ?> CSS Files</legend>
	<?php
	$dirPath = TEMPLATEPATH. '/styles/' .$_SESSION['style'];
	if ($handle = opendir($dirPath)) {
		while (false !== ($file = readdir($handle))) {
			if ($file != "." && $file != "..") {
				if (is_file("$dirPath/$file")) {
	?>
	<div class="csslist">
	<?php if ($file == 'stylesheet.css') : ?>
	<a href="?page=<?php echo $_GET['page']; ?>&amp;style=<?php echo $this->options["style"]; ?>&amp;css=<?php echo remove_ext($file); ?>&amp;do=edit"><b><?php echo $file; ?></b></a> (Core stylesheet)
	<?php else : ?>
	<a href="?page=<?php echo $_GET['page']; ?>&amp;style=<?php echo $this->options["style"]; ?>&amp;css=<?php echo remove_ext($file); ?>&amp;do=edit"><?php echo $file; ?></a>
	<?php endif; ?>
	</div>
	<?php
				}
			}
		}
	closedir($handle);
	}
	?>
	</fieldset>
	
	<?php if (!empty($_GET['css'])) { ?>

	<?php
	$lines = file(TEMPLATEPATH.'/styles/'. $_SESSION['style'] .'/'. $_GET['css'] .'.css');
	print '<h1 style="font-weight: normal;font-family: georgia;"><em>Editing '.$_GET["css"].'.css</em></h2>';
	print '<textarea class="resizable" name="css_c" id="css_c">';
	foreach($lines as $line) {
		print $line;
	}
	print '</textarea>';
	?>
	
	<?php } ?>
	<?php } ?>
	
	<?php if (!empty($msg)) print '<p>'.$msg.'</p>'; ?>

	</div>